import {useUserStore} from "@/stores/user.store.ts";
import type {Directive, App} from "vue";


/**
 * 自定义权限指令
 */
const permissionDirective: Directive<HTMLElement, string | string[]> = {
    mounted(el, binding) {
        const signStore = useUserStore();
        const {value} = binding;

        // 判断是否拥有权限
        const hasPermission = Array.isArray(value)
            ? value.some(permission => signStore.userPermissions?.includes(permission))
            : signStore.userPermissions?.includes(value)

        // 没有权限，卸载组件
        if (!hasPermission) {
            el.parentNode?.removeChild(el);
        }
    }
}


/**
 * 注册自定义指令
 * @param app
 */
export function setupDirective(app: App) {
    app.directive("permission", permissionDirective);
}
